//
//  addServerViewController.m
//  changAnCommercial
//
//  Created by 高文富 on 16/11/28.
//  Copyright © 2016年 高文富. All rights reserved.
//

#import "addServerViewController.h"
//#import "MMProgressHUD.h"
#import "MBProgressHUD+Add.h"
@interface addServerViewController ()

@end

@implementation addServerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addRightBarButton];
    if([self.title isEqualToString:@"修改服务器"]){
    
        _nickName.text=[_dic objectForKey:@"serverName"];
        _addrass.text=[_dic objectForKey:@"serverAddress"];

    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}
- (void)addRightBarButton
{
    //  添加切换城市点击事件
    UIButton *rightButton = [[UIButton alloc] initWithFrame: CGRectMake(0, 0, 25, 25)];
    [rightButton addTarget:self action:@selector(addSever:) forControlEvents:UIControlEventTouchUpInside];
    [rightButton setBackgroundColor:[UIColor clearColor]];
    [rightButton setImage:[UIImage imageNamed:@"ico_common_tj3"] forState:UIControlStateNormal];
    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = rightButtonItem;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
   [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
}


-(void)addSever:(UIButton *)sender {
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
    if (_addrass.text.length==0 || _nickName.text.length==0) {
        
           [MBProgressHUD showError:@"名称或者地址不能为空" toView:self.view];
       
    }else{
        
        if ([self.title isEqualToString:@"修改服务器"]) {
            if (_delegate && [_delegate respondsToSelector:@selector(updateValue:serverAddress:)])
            {
                [_delegate updateValue:_nickName.text serverAddress:_addrass.text];
                [self.navigationController popViewControllerAnimated:YES];
            }

        }else{
        
            if (_delegate && [_delegate respondsToSelector:@selector(updateServerData:address:)])
            {
                [_delegate updateServerData:_nickName.text address:_addrass.text];
                [self.navigationController popViewControllerAnimated:YES];
            }

        }
        
        
        
    }
    
    
    
}

#pragma mark-UITextField
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField==_nickName) {
        [_addrass becomeFirstResponder];
     
    }else{
    
     [self addSever:nil];
    }
   
    
    return YES;
}

@end
